#include "View_qt.h"

View_qt::View_qt(QGraphicsScene* scene) :
    QGraphicsView(scene),
    scale_times(0),
    maxScale(2.0),
    minScale(0.07) {
}

void View_qt::keyPressEvent(QKeyEvent *event)
{
    // qreal dx = 0, dy = 0;

    // // 根据按下的键调整视野的偏移
    // switch (event->key()) {
    // case Qt::Key_W:
    //     dy = -10;
    //     break;
    // case Qt::Key_S:
    //     dy = 10;
    //     break;
    // case Qt::Key_A:
    //     dx = -10;
    //     break;
    // case Qt::Key_D:
    //     dx = 10;
    //     break;
    // default:
    //     // 其他按键不处理
    //     QGraphicsView::keyPressEvent(event);
    //     return;
    // }

    // horizontalScrollBar()->setValue(horizontalScrollBar()->value() + dx);
    // verticalScrollBar()->setValue(verticalScrollBar()->value() + dy);

    emit keyPress(event);
    // 调用基类的处理函数
    QGraphicsView::keyPressEvent(event);
}

void View_qt::wheelEvent(QWheelEvent *event){
    // Zoom the view
    int delta = event->angleDelta().y();
    if(delta == 0){return;}
    qreal scaleFactor = 1.2;
    if (delta > 0) {
        scale(scaleFactor, scaleFactor);
    } else {
        scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    }
    qreal currentScaleFactor = transform().m11();
    if(currentScaleFactor > maxScale){
        scale(maxScale/currentScaleFactor,maxScale/currentScaleFactor);
    }
    if(currentScaleFactor < minScale){
        scale(minScale/currentScaleFactor,minScale/currentScaleFactor);
    }
    // 不要调用基类的处理函数 否则滚轮会移动（
    // QGraphicsView::wheelEvent(event);
    emit mouseWheel(event);
}

void View_qt::mouseMoveEvent(QMouseEvent *event){
    // // qDebug() << "in View mouse move";
    emit mouseMove(event);
}

View_qt::~View_qt(){

}
